package com.qf.fmall2202.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.entity.ProductSku;
import com.qf.fmall2202.product.service.IProductImgService;
import com.qf.fmall2202.product.service.IProductService;
import com.qf.fmall2202.product.service.IProductSkuService;
import com.qf.fmall2202.shopcart.entity.ShoppingCart;
import com.qf.fmall2202.shopcart.mapper.ShoppingCartMapper;
import com.qf.fmall2202.shopcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author lzh
 * @since 2022-11-08
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    IProductService productService;

    @Autowired
    IProductImgService productImgService;

    @Autowired
    IProductSkuService productSkuService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listCids(String cids) {

                //1. cids 按逗号分隔进行切分
        final String[] cidArray = cids.split(",");

        //2. 查询 cidArray 对应的 购物车中的 信息
        final List<ShoppingCart> shoppingCarts = this.
                list(new QueryWrapper<ShoppingCart>().
                        in("cart_id", Arrays.asList(cidArray)));

        //3. 填充缺失 的信息
        populateCart(shoppingCarts);

        return shoppingCarts;


    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShoppingCart> listByUserId(Integer userId) {
                //1.查询当前用户购物信息
        List<ShoppingCart> shopcartList = this.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, userId));


        //2.补全 shopcart 缺少的信息
        populateCart(shopcartList);
        return shopcartList;
    }

    public void populateCart(List<ShoppingCart> shopcartList) {

        //1.查商品表3
        //List<shopcart> ---->List<商品ID>
        List<String> productIdList = shopcartList.stream().map(shoppingCart ->
                shoppingCart.getProductId())
                .collect(Collectors.toList());
        Map<String, String> productMap = productService
                .list(new QueryWrapper<Product>()
                        .in("product_id", productIdList))
                .stream()
                .collect(Collectors.toMap(Product::getProductId, Product::getProductName));


        //2.查商品图片表
        Map<String, String> productImgMap = productImgService.list(new QueryWrapper<ProductImg>()
                .in("is_main", 1)
                .in("item_id", productIdList))
                .stream()
                .collect(Collectors.toMap(ProductImg::getItemId, ProductImg::getUrl));


        //3.查sku表
        //List<shopcart> ---->List<skuID>
        List<String> skuIdList = shopcartList.stream()
                .map(ShoppingCart::getSkuId)//直接写要转换的类型
                .collect(Collectors.toList());
        Map<String, ProductSku> productValueMap = productSkuService.list(new QueryWrapper<ProductSku>()
                .in("sku_id", skuIdList))
                .stream()
                .collect(Collectors.toMap(ProductSku::getSkuId, productSku -> productSku));


        shopcartList.forEach(shoppingCart -> {
            shoppingCart.setProductName(productMap.get(shoppingCart.getProductId()));
            shoppingCart.setProductImg(productImgMap.get(shoppingCart.getProductId()));


            /**
             *
             *
             */
            ProductSku productSku = productValueMap.get(shoppingCart.getSkuId());
            shoppingCart.setSkuName(productSku.getSkuName());
            shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
            shoppingCart.setSellPrice(productSku.getSellPrice());
            shoppingCart.setStock(productSku.getStock());
        });
    }
}
